package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JTextArea;

import controller.DataBaseInteactor;

import model.Conference;
import model.Reviewer;

/**
 * The view of the user when he/she login as Reviewer role.
 * 
 * @author Richard Lee
 * @version 52813
 */
public class ReviewerView extends JFrame {

	/**
	 * Default serial  version id.
	 */
	private static final long serialVersionUID = -7596031677896009551L;
	
	/**
	 * The conference.
	 */
	private Conference conference;
	
	/**
	 * GUI component.
	 */
	private final Font titleFont = new Font("Verdana", Font.BOLD, 20);
	private final JLabel titleLabel = new JLabel();
	private final JLabel dateLabel = new JLabel();
	private final JPanel buttonsPanel = new JPanel(new FlowLayout());
	private final JPanel reviewPanel = new JPanel(new GridLayout(11,2));
	private final JPanel titlePanel = new JPanel(new BorderLayout());
	private final JButton submitButton = new JButton("Submit Review");
	private final JButton logoutButton = new JButton("Log out");
	private final JButton viewButton = new JButton("View");
	final JRadioButton radio1 = new JRadioButton("5", false);
	private JTextArea summaryArea = new JTextArea();
	private JTable manTabel;
	private ArrayList<JPanel> radioPanelList = new ArrayList<JPanel>();
	private ArrayList<ButtonGroup> buttonGroupList = new ArrayList<ButtonGroup>();
	private String[] columnNames = {"Title", "Author", "Stauts", "Review", "Recomendation"};
	private Object[][] manuscriptData = {{"Title1", "Ying", "NOT REVIEWED", "YES", "NO"},
			{"Title2", "Yun", "ACCEPTED", "YES", "YES"}};
	
	/**
	 * Database interactor.
	 */
	private final DataBaseInteactor inteactor;

	/**
	 * user id.
	 */
	private final String id;
	
	/**
	 * List of questions for the review.
	 */
	private String[] questionList = {"Can the content be directly applied by classroom instructors or curriculum designers? ",
			"Does the work appeal to a broad readership interested in engineering education or is it narrowly specialized?",
			"Does the work address a significant problem?",
			"Does the author build upon relevant references and bodies of knowledge",
			"If a teaching intervention is reported, is it adequately evaluated in terms of its impact on learning in actual use?",
			"Does the author use methods appropriate to the goals, both for the instructional intervention and the evaluation of impact on learning?",
			"Did the author provide sufficient detail to replicate and evaluate? ",
			"Is the paper clearly and carefully written?",
			"Does the paper adhere to accepted standards of style, usage, and composition? ",
			"Summary Rating"};
	
	/**
	 * Constructor.
	 * 
	 * @param theConf the conference user belongs to.
	 * @param theInteactor database interactor
	 * @param theId user id.
	 */
	public ReviewerView(Conference theConf, DataBaseInteactor theInteactor, String theId) {
		super("Reviwer View");
		conference = theConf;
		inteactor = theInteactor;
		id = theId;
	}
	
	/**
	 * Combine and add all the GUI component.
	 */
	public void createReviewerView() {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		StringBuilder sb = new StringBuilder();
		sb.append("Manuscript Due Date: ");
		sb.append(conference.getManDate());
		sb.append(" Review Due Date: ");
		sb.append(conference.getreviewDate());
		sb.append(" Recommendation Due Date: ");
		sb.append(conference.getRecomDate());
		dateLabel.setText(sb.toString());
		titleLabel.setText(conference.getName());
		titleLabel.setFont(titleFont);
		titlePanel.add(titleLabel, BorderLayout.CENTER);
		titlePanel.add(dateLabel, BorderLayout.PAGE_END);
		p.add(titlePanel);

		manTabel = new JTable(manuscriptData, columnNames);
		buttonsPanel.add(viewButton);
		buttonsPanel.add(submitButton);
		buttonsPanel.add(logoutButton);
		
		/**
		 * Make a 10 radio buttons for the review.
		 */
		for (int i = 0; i < 10; i++) {
			radioPanelList.add(i, new JPanel());
			buttonGroupList.add(i, new ButtonGroup());
			for (int j = 1; j < 6; j++) {
				JRadioButton buttonToAdd = new JRadioButton(new Integer(j).toString(), false);
				buttonToAdd.setActionCommand(new Integer(j).toString());
				buttonGroupList.get(i).add(buttonToAdd);
				radioPanelList.get(i).add(buttonToAdd);
			}
			reviewPanel.add(new JLabel(questionList[i]));
			reviewPanel.add(radioPanelList.get(i));
		}
		JLabel summaryLabel = new JLabel("Summary Comments");
		summaryArea.setBorder(BorderFactory.createLineBorder(Color.black));
		
		
		reviewPanel.add(summaryLabel);
		reviewPanel.add(summaryArea);
		reviewPanel.setBorder(BorderFactory.createTitledBorder("Review"));
		
		p.add(manTabel.getTableHeader());
		p.add(manTabel);
		p.add(reviewPanel);
		p.add(buttonsPanel);
		add(p);
		pack();
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		actionButton();
	}
	
	/**
	 * Add actions to buttons.
	 */
	private void actionButton() {
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Reviewer reviewer = new Reviewer(id, inteactor);
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				ArrayList<String> reviewList = new ArrayList<String>();
				for (int i = 0; i < 10; i++) {
					reviewList.add(buttonGroupList.get(i).getSelection().getActionCommand());
				}
				reviewList.add(summaryArea.getText());
				reviewer.submitReview(id, conference, author, manTitle, reviewList);
			}
		});
		
		logoutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				LogInFrame loginFrame = new LogInFrame(inteactor);
				loginFrame.setVisible(true);
			}
		});
		
		viewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				ViewView view = new ViewView(author, manTitle, conference, inteactor);
				view.createViewView();
			}
		});
	}
	
}
